﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;
using WaveSim.Core.Equations.Galerkin;

namespace WaveSim.Extensions.LabView.Providers
{
    public class Galerkin2DProvider
    {
        private Galerkin2DEquation eq;

        public Galerkin2DProvider(Galerkin2DEquation eq)
        {
            this.eq = eq;
        }

        public int Nx { get; set; }

        public Complex[,] Ey(Complex[,] coeff)
        {
            int nz = coeff.GetLength(0);

            Complex[,] result = new Complex[Nx, nz];

            for (int zIndex = 0; zIndex < nz; zIndex++)
            {
                Complex[] arr = new Complex[coeff.GetLength(1)];
                for (int i = 0; i < arr.Length; i++)
                {
                    arr[i] = coeff[zIndex, i];
                }

                for (int i = 0; i < Nx; i++)
                {
                    double x = i * (eq.WaveGuide.A / (Nx - 1));
                    result[i, zIndex] = eq.Ey(arr, x);
                }
            }

            return result;
        }

        public double[,] ToMagnitude(Complex[,] Ey)
        {
            double[,] mag = new double[Ey.GetLength(0), Ey.GetLength(1)];

            for (int i = 0; i < mag.GetLength(0); i++)
            {
                for (int j = 0; j < mag.GetLength(1); j++)
                {
                    mag[i, j] = Ey[i, j].Magnitude;
                }
            }

            return mag;
        }

        public double[,] ToReal(Complex[,] Ey)
        {
            double[,] real = new double[Ey.GetLength(0), Ey.GetLength(1)];

            for (int i = 0; i < real.GetLength(0); i++)
            {
                for (int j = 0; j < real.GetLength(1); j++)
                {
                    real[i, j] = Ey[i, j].Real;
                }
            }

            return real;
        }

        public double[,] ToImage(Complex[,] Ey)
        {
            double[,] img = new double[Ey.GetLength(0), Ey.GetLength(1)];

            for (int i = 0; i < img.GetLength(0); i++)
            {
                for (int j = 0; j < img.GetLength(1); j++)
                {
                    img[i, j] = Ey[i, j].Imaginary;
                }
            }

            return img;
        }

        public double[,] ToPhase(Complex[,] Ey)
        {
            double[,] phase = new double[Ey.GetLength(0), Ey.GetLength(1)];

            for (int i = 0; i < phase.GetLength(0); i++)
            {
                for (int j = 0; j < phase.GetLength(1); j++)
                {
                    phase[i, j] = Ey[i, j].Phase;
                }
            }

            return phase;
        }
    }
}
